﻿function cascadeDropDown(source, target,action) {
    var select = $('#' + source);
    
    var selectDeptCode = select.val();
    $.getJSON(action, { deptCode: selectDeptCode }, function (dept) {
        var childDeptSelect = $('#' + target);
        childDeptSelect.empty();
        $.each(dept, function (index, dept) {
            childDeptSelect.append(
                $('<option/>')
                    .attr('value', dept.Value)
                    .text(dept.Text)
            );
        });
    });
};

function isDate(dateString) {
    if (dateString == "") return true;
    var r = dateString.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (r == null) {
        return false;
    }
    var d = new Date(r[1], r[3] - 1, r[4]);
    var num = (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
    if (num == 0) {
    }
    return (num != 0);
};

function setDriverAutoComplete(ui, dept, service_url, select_fun) {
    //$("#" + ui).change(function () {
    //    $("#" + ui)
    //});
    $('#' + ui).autocomplete({
        source: function (request, response) {
            $.ajax({
                url: service_url,
                dataType: "json",
                data: {
                    name_startsWith: request.term,
                    dept_code: dept
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.CAPTION,
                            value: item.MAN_NAME,
                            key: item.MAN_CODE
                        }
                    }));
                }

            });
        },
        minLength: 1,
        select: select_fun,
        delay: 0
    });
};
function setCarAutoComplete(ui,dept, service_url, select_fun) {
    $("#"+ui).autocomplete({
        source: function (request, response) {
            $.ajax({
                url: service_url,
                dataType: "json",
                data: {
                    deptcode: dept,
                    name_startsWith: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.CAPTION,
                            value: item.RECORD_NO,
                            key: item.CAR_NO
                        }
                    }));
                }

            });
        },
        minLength: 1,
        select: select_fun,
        delay: 0
    });
};


function getFileSize(obj) {
    var objValue = obj.value;
    if (objValue == "") return;
    var fileLenth = -1;
    try {
        //对于IE判断要上传的文件的大小
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        fileLenth = parseInt(fso.getFile(objValue).size);
    } catch (e) {
        try {
            //对于非IE获得要上传文件的大小
            fileLenth = parseInt(obj.files[0].size);
        } catch (e) {
            fileLenth = -1;

        }

    }

    return fileLenth / 1024;
};
function check_file(file_id, allow_ext,max_size) {
    var file = $("#" + file_id).val();
    if (file == null || file == "") {
        return true;
    }
    var extend = file.substring(file.lastIndexOf(".") + 1).toLowerCase();
    
    if (allow_ext.indexOf(extend + "|", 0) < 0) {
        alert("该格式的附件不能上传。");
        return false;
    }
    var size = getFileSize(document.getElementById(file_id));
    if (size > max_size) {
        alert("文件大小超过限制，每个文件限制为最大"+max_size+"KB。");
        return false;
    }
    return true;
};

Array.prototype.indexOf = function (e) {
    for (var i = 0, j; j = this[i]; i++) {
        if (j == e) { return i; }
    }
    return -1;
};